<script setup lang="ts">
import useLoginStore from '@/stores/login'
import { mapPathToMenu } from '@/utils/map-menus'
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})
const loginMenu = useLoginStore()
const userMenus = loginMenu.userMenus

const router = useRouter()
function handleItemClick(item: any) {
  console.log(item)
  const url = item.url
  router.push(url)
}
// 默认菜单
const route = useRoute()
const defaultActive = computed(() => {
  const pathMenu = mapPathToMenu(route.path, userMenus)
  return pathMenu.id + ''
})
</script>

<template>
  <div class="main-menu">
    <div class="logo">
      <img class="img" src="@/assets/img/logo.svg" alt="">
      <h2 v-show="!isCollapse" class="title">Coderlem-CMS</h2>
    </div>
    <div class="menu">
      <el-menu :default-active="defaultActive" text-color="#b7bdc3" active-text-color="#fff" background-color="#001529"
        :collapse="isCollapse">
        <template v-for="(item,index) in userMenus" :key="index">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 动态组件 -->
              <el-icon>
                <component :is="item.icon.split('-icon-')[1]" />
              </el-icon>
              <span>{{item.name}}</span>
            </template>
            <template v-for="(subitem,index) in item.children" :key="index">
              <el-menu-item :index="subitem.id + ''" @click="handleItemClick(subitem)">{{subitem.name}}</el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
      <!-- <el-menu text-color=" #b7bdc3" active-text-color="#fff" background-color="#001529">
                <el-sub-menu index="1">
                  <template #title>
                    <el-icon>
                      <Monitor />
                    </el-icon>
                    <span>系统总览</span>
                  </template>
                  <el-menu-item index="1-1">核心技术</el-menu-item>
                  <el-menu-item index="1-2">商品统计</el-menu-item>
                </el-sub-menu>

                <el-sub-menu index="2">
                  <template #title>
                    <el-icon>
                      <Setting />
                    </el-icon>
                    <span>系统管理</span>
                  </template>
                  <el-menu-item index="2-1">用户管理</el-menu-item>
                  <el-menu-item index="2-2">部门管理</el-menu-item>
                  <el-menu-item index="2-3">菜单管理</el-menu-item>
                  <el-menu-item index="2-4">角色管理</el-menu-item>
                </el-sub-menu>

                <el-sub-menu index="3">
                  <template #title>
                    <el-icon>
                      <Goods />
                    </el-icon>
                    <span>商品中心</span>
                  </template>
                  <el-menu-item index="3-1">商品类别</el-menu-item>
                  <el-menu-item index="3-2">商品信息</el-menu-item>
                </el-sub-menu>

                <el-sub-menu index="4">
                  <template #title>
                    <el-icon>
                      <Monitor />
                    </el-icon>
                    <span>随便聊聊</span>
                  </template>
                  <el-menu-item index="4-1">你的故事</el-menu-item>
                  <el-menu-item index="4-2">故事列表</el-menu-item>
                  <el-sub-menu index="4">
                    <template #title>音乐分享</template>
                    <el-menu-item index="4-3">我的推荐</el-menu-item>
                    <el-menu-item index="4-4">热榜推荐</el-menu-item>
                  </el-sub-menu>
                </el-sub-menu>
      </el-menu> -->
    </div>
  </div>
</template>
<style lang="less" scoped>
.main-menu {
  height: 100%;
  background-color: #001529;
}

.logo {
  display: flex;
  height: 28px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;

  .img {
    height: 100%;
    margin: 0 10px;
  }

  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}

.el-menu {
  border-right: none;
  user-select: none;
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
